
package com.jf.cloud.product.listener;

import com.jf.cloud.api.product.dto.SkuStockLockDTO;
import com.jf.cloud.common.order.bo.RefundReductionStockBO;
import com.jf.cloud.common.product.constant.StockBillType;
import com.jf.cloud.common.product.constant.StockType;
import com.jf.cloud.common.rocketmq.config.RocketMqConstant;
import com.jf.cloud.product.service.SkuStockService;
import com.jf.cloud.product.service.StockBillLogService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 解锁库存的监听
 * @author zz
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.ORDER_REFUND_SUCCESS_STOCK_TOPIC,consumerGroup = RocketMqConstant.ORDER_REFUND_SUCCESS_STOCK_TOPIC)
public class OrderRefundSuccessStockConsumer implements RocketMQListener<List<RefundReductionStockBO>> {

    @Autowired
    private SkuStockService skuStockService;
    @Autowired
    private StockBillLogService stockBillLogService;

    /**
     * 订单退款导致待发货订单取消，需要还原优惠券和库存
     */
    @Override
    public void onMessage(List<RefundReductionStockBO> refundReductionStocks) {
        skuStockService.reductionStock(refundReductionStocks);
        List<SkuStockLockDTO> skuStockLockList = new ArrayList<>();
        if(Objects.equals(refundReductionStocks.get(0).getSupplierProd(),1)){
            return;
        }
        // 生成入库明细
        for (RefundReductionStockBO refundReductionStock : refundReductionStocks) {
            SkuStockLockDTO skuStockLockDTO = new SkuStockLockDTO();
            skuStockLockDTO.setCount(refundReductionStock.getCount());
            skuStockLockDTO.setShopId(refundReductionStock.getShopId());
            skuStockLockDTO.setOrderId(refundReductionStock.getOrderId());
            skuStockLockDTO.setStatus(-1);
            skuStockLockDTO.setSkuId(refundReductionStock.getSkuId());
            skuStockLockDTO.setSpuId(refundReductionStock.getSpuId());
            skuStockLockList.add(skuStockLockDTO);
        }
        // 生成入库明细信息
        stockBillLogService.generateStockLogOfOrderList(skuStockLockList, StockBillType.RETURN_STORAGE.value(), StockType.WAREHOUSING.value());
    }
}
